//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics;
using System.Resources;
using OpenLS.Spreadsheet.Extensibility;

namespace OpenLS.Spreadsheet.StandardAddIn
{
    public class Localizer : IFunctionLocalizer
    {
        private readonly ResourceManager _resourceManager =
            new ResourceManager("OpenLS.Spreadsheet.StandardAddIn.FunctionTable", typeof (Localizer).Assembly);

        private readonly Dictionary<string, string> _globalToLocal = new Dictionary<string, string>();
        private readonly Dictionary<string, string> _localToGlobal = new Dictionary<string, string>();

        #region IFunctionLocalizer Members

        string IFunctionLocalizer.GetLocalName(string name)
        {
            string result;
            if (_globalToLocal.TryGetValue(name, out result))
                return result;
            return name;
        }

        string IFunctionLocalizer.GetGlobalName(string name)
        {
            string result;
            if (_localToGlobal.TryGetValue(name, out result))
                return result;
            return name;
        }


        public void Initialize(IEnumerable<string> globalNames)
        {
           // Debug.WriteLine("Kherty Spreadsheet", "Initializing " + GetType());
            foreach (string globalName in globalNames)
            {
                string localName = _resourceManager.GetString(globalName);
                if (localName == null)
                    continue;
                _globalToLocal.Add(globalName, localName);
                _localToGlobal.Add(localName, globalName);
            }
#if TESTFUNCTIONS
            string toPrint = "";
            foreach (Function f in addIn.GetFunctions())
            {
                string result;
                if (!globalToLocal.TryGetValue(f.Name, out result))
                    toPrint += f.Name + ",";
            }
            Debug.WriteLine("Unable to find " + toPrint);
#endif
        }

        #endregion
    }
}
